package com.ruoyi.manage.enterprise.controller;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.ObjUtil;
import com.ruoyi.common.utils.StrUtil;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.manage.enterprise.domain.EcEnterpriseContact;
import com.ruoyi.manage.enterprise.domain.vo.EcEnterpriseContactVo;
import com.ruoyi.manage.enterprise.service.IEcEnterpriseContactService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
/**
 * 企业联系人Controller
 *
 * @author ruoyi
 * @date 2025-08-14
 */
@Api(tags="企业联系人控制器")
@RestController
@RequestMapping("/manage/contact" )
public class EcEnterpriseContactController extends BaseController {
    /**
    * 配置表别名 默认空
    */
    private final static String TABLE_PREFIX=StrUtil.initTableAlias(null);
    @Autowired
    private IEcEnterpriseContactService ecEnterpriseContactService;

    /**
     * 查询企业联系人列表
     */
    @ApiOperation(value = "查询企业联系人",notes = "企业联系人管理")
//    @PreAuthorize("@ss.hasPermi('manage:contact:list')" )
    @GetMapping("/list" )
    public TableDataInfo list(EcEnterpriseContactVo ecEnterpriseContact,QueryWrapper<EcEnterpriseContact> queryWrapper) {
        setQueryCondition(ecEnterpriseContact,queryWrapper);
        startPage();
        List<EcEnterpriseContact> list = ecEnterpriseContactService.list(queryWrapper);
        return getDataTable(list);
    }
    private void setQueryCondition(EcEnterpriseContactVo ecEnterpriseContact, QueryWrapper<EcEnterpriseContact> queryWrapper) {
        if(StrUtil.isNotBlank(ecEnterpriseContact.getIdCard())){
            queryWrapper.eq(TABLE_PREFIX+"id_card" ,ecEnterpriseContact.getIdCard());
        }
        if(ObjUtil.isNotNull(ecEnterpriseContact.getEnterpriseId())){
            queryWrapper.eq(TABLE_PREFIX+"enterprise_id" ,ecEnterpriseContact.getEnterpriseId());
        }
        queryWrapper.orderByDesc(TABLE_PREFIX+"create_time");
    }

    /**
     * 导出企业联系人列表
     */
    @PreAuthorize("@ss.hasPermi('manage:contact:export')" )
    @PostMapping("/export" )
    public void export(HttpServletResponse response, EcEnterpriseContactVo ecEnterpriseContact,QueryWrapper<EcEnterpriseContact> queryWrapper) {
        if (CollUtil.isEmpty( ecEnterpriseContact.getIds())){
            setQueryCondition(ecEnterpriseContact,queryWrapper);
        }else {
            queryWrapper.in(TABLE_PREFIX+"id",ecEnterpriseContact.getIds());
        }
        List<EcEnterpriseContact> list = ecEnterpriseContactService.list(queryWrapper);
        ExcelUtil<EcEnterpriseContact> util = new ExcelUtil<EcEnterpriseContact>(EcEnterpriseContact. class);
        util.exportExcel(response,list, "contact");
    }

    /**
     * 获取企业联系人详细信息
     */
    @ApiOperation(value = "获取企业联系人",notes = "企业联系人管理")
    @PreAuthorize("@ss.hasPermi('manage:contact:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(ecEnterpriseContactService.queryGetById(id));
    }

    /**
     * 新增企业联系人
     */
    @ApiOperation(value = "新增企业联系人",notes = "企业联系人管理")
//    @PreAuthorize("@ss.hasPermi('manage:contact:add')" )
    @RepeatSubmit
    @PostMapping
    public AjaxResult add(@Validated @RequestBody EcEnterpriseContact ecEnterpriseContact) {
        return toAjax(ecEnterpriseContactService.addEcEnterpriseContact(ecEnterpriseContact) ? 1 : 0);
    }

    /**
     * 修改企业联系人
     */
    @ApiOperation(value = "修改企业联系人",notes = "企业联系人管理")
//    @PreAuthorize("@ss.hasPermi('manage:contact:edit')" )
    @PutMapping
    public AjaxResult edit(@RequestBody EcEnterpriseContact ecEnterpriseContact) {
        return toAjax(ecEnterpriseContactService.updateEcEnterpriseContact(ecEnterpriseContact) ? 1 : 0);
    }

    /**
     * 删除企业联系人
     */
    @ApiOperation(value = "删除企业联系人",notes = "企业联系人管理")
//    @PreAuthorize("@ss.hasPermi('manage:contact:remove')" )
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(ecEnterpriseContactService.deleteEcEnterpriseContactByIds(ids) ? 1 : 0);
    }
}
